package com.jf.blogbackened.service.impl;

import com.alibaba.fastjson.JSON;
import com.jf.blogbackened.bean.Admin;
import com.jf.blogbackened.bean.PageBean;
import com.jf.blogbackened.bean.Result;
import com.jf.blogbackened.bean.User;
import com.jf.blogbackened.mapper.ArticleMapper;
import com.jf.blogbackened.mapper.UserMapper;
import com.jf.blogbackened.service.UserService;
import com.jf.blogbackened.utils.MD5Utils;
import com.jf.blogbackened.utils.MyJwtUtil;
import com.jf.blogbackened.utils.OSSUtil;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

/**
 * @author David
 */
@Service
public class UserServiceImpl implements UserService {
    @Resource
    private UserMapper userMapper;
    @Resource
    private ArticleMapper articleMapper;

    @Override
    public Result<Admin> login(Admin admin) {
        // 给密码进行二次加密
        String password = admin.getPassword();
        password = MD5Utils.md5(password);
        admin.setPassword(password);
        Admin adminInfo = userMapper.getAdminByUsernameAndPwd(admin);
        if (adminInfo == null) {
            return Result.error().put("message","用户名或密码不正确");
}
        // 为Admin对象添加token
        String token = MyJwtUtil.createJWT(JSON.toJSONString(adminInfo));
        adminInfo.setToken(token);
        return Result.ok().put("data",adminInfo);
    }

    @Override
    public PageBean<User> getAllUser(User params, Integer currentPage, Integer pageSize) {
        // 1 获取总的记录数
        Integer total=userMapper.getCount(params);
        // 获取总页数(99 10  10页 99/10 9+1=10  100条 10条
        Integer totalPage = total%pageSize==0?total / pageSize : (total / pageSize)+1;
        // 计算偏移量
        Integer offset = (currentPage - 1) * pageSize;
        List<User> userList=userMapper.getPageData(params,offset,pageSize);
    PageBean<User> pb = new PageBean<>(userList, total, pageSize);
        pb.setCurrentPage(currentPage);
        pb.setPageSize(pageSize);
        pb.setPageCount(totalPage);
        return pb;
    }

    @Override
    public Result<User> addOrUpdateUser(User user) {
        int res=0;
        System.out.println(user.getId());
        if(user.getId()==null){
            // 重名校验
            String nickname = user.getNickname();
            int i=userMapper.getCountByNickname(nickname);
            if (i>0){
                // 说明已经有该用户存在
                return Result.error().put("message","已经存在该用户，无需重复添加");
            }
            // 加密密码
            String password = user.getPassword();
            user.setPassword(MD5Utils.md5(password));
            // 插入数据库
            res =userMapper.insert(user);
        }else {
           res= userMapper.update(user);
        }
        if (res > 0) {
            return Result.ok().put("data",Integer.toString(res));
        }else {
            return Result.error().put("message","服务器异常");
        }
    }

    @Override
    public Result<User> getUserById(Integer id) {
        if (id == null){
            return Result.error().put("message","参数不正确");
        }
        User user =userMapper.getUserById(id);
       if (user == null) {
           return Result.error().put("message","用户不存在");
       }
       return Result.ok().put("data",user);
    }

    @Override
    public Result<User> deleteUserById(Integer id) {
        // 先删除用户的文章
        articleMapper.delArticleByUserId(id);
         User user = userMapper.getUserById(id);
        String avatar = user.getAvatar();
        OSSUtil.del(avatar);
        Integer i=userMapper.deleteUserById(id);
        if (i > 0) {
            return Result.ok().put("data",Integer.toString(i));
        }else {
            return Result.error().put("message","服务器异常");
        }
    }

    @Override
    public Result getAllUsers() {
        List<User> userList = userMapper.getAllUsers();
        return Result.ok().data(userList);
    }
}
